/**
 * 名晟中鑫是一家专业数字化、智慧化研发和运营的服务提供商，公司本着“星火燎原、创新创业、科技顶天、服务立地”的发展理念，
 * 致力于新型智慧城市的建设和运营，业务包括智慧城市顶层设计咨询、互联网+、智慧政务、智慧医疗、智慧社区、智慧交通、智慧农业、智慧教育、智慧应急等。
 * 积极构建以政府为主导，企业和市民为主体、市场为导向、产学研相结合的推进体系，资源有效整合与共享，不断增强建设智慧城市建设的整体合力，
 * 真正推动民众生存智慧化。
 **/
package com.zj.controller;

import com.google.code.kaptcha.Producer;
import com.zj.controller.request.SendCodeRequest;
import com.zj.result.R;
import com.zj.util.StringOperationUtils;
import com.zj.utils.CommonUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName NotifyController
 * @Description notify
 * @createTime 2024年12月03日 21:57
 */
@RestController
@RequestMapping("/api/account")
@Slf4j
public class NotifyController {

    @Autowired
    private Producer captchaProducer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final long CAPTCHA_CODE_EXPIRED = 1000*10*60;

    @GetMapping("/send_code")
    public void sendData(HttpServletRequest request, HttpServletResponse response){
        String captchaText = captchaProducer.createText();
        log.info(captchaText);
        redisTemplate.opsForValue().set(getKey(request),captchaText,CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS );
        log.info("get key{}",redisTemplate.opsForValue().get(getKey(request)));
        BufferedImage image = captchaProducer.createImage(captchaText);
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            boolean img = ImageIO.write(image, "jpg",outputStream);
            outputStream.flush();
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    @PostMapping("send")
    public R send(SendCodeRequest request,HttpServletRequest servletRequest){
        String key = getKey(servletRequest);
        if (StringOperationUtils.isNotEmpty(key) && StringOperationUtils.isNotEmpty(request.getCaptcha())){
            String captcha = redisTemplate.opsForValue().get(key);
            if (captcha.equals(request.getCaptcha())){
                //true
                redisTemplate.delete(key);

                return R.success("operation has succcessed");
            }else {
                return R.error("captcha has expired or not correct");
            }
        }else {
            return  R.error("captcha can not be empty");
        }
    }

    private String getKey(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");

        String key = "account-service:captcha:"+ CommonUtils.MD5(userAgent);
        log.info("captcha key{}",key);
        return key;
    }

}
